package com.base.api.service.selfApplyRefund.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.base.api.dto.selfApplyRefund.ApplyRefundDTO;
import com.base.api.service.selfApplyRefund.SelfApplyRefundApiService;
import com.base.mapper.voipmapper.SubscribersMapper;
import com.base.po.voip.Subscribers;
import com.base.result.ResponseResult;
import com.base.result.ResponseResultUtils;
import com.base.self.common.SelfRuntimeException;
import com.base.self.entity.SelfApplyRefund;
import com.base.self.entity.SelfOrder;
import com.base.self.mapper.SelfApplyRefundMapper;
import com.base.self.mapper.SelfOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
public class SelfApplyRefundApiServiceImpl implements SelfApplyRefundApiService {
    private final SelfApplyRefundMapper selfApplyRefundMapper;

    private final SelfOrderMapper selfOrderMapper;

    private final SubscribersMapper subscribersMapper;

    public SelfApplyRefundApiServiceImpl(SelfApplyRefundMapper selfApplyRefundMapper,
                                         SelfOrderMapper selfOrderMapper,
                                         SubscribersMapper subscribersMapper) {
        this.selfApplyRefundMapper = selfApplyRefundMapper;
        this.selfOrderMapper = selfOrderMapper;
        this.subscribersMapper = subscribersMapper;
    }

    @Override
    @Transactional
    public ResponseResult applyRefund(ApplyRefundDTO applyRefundDTO) {
        final Subscribers subscribers = subscribersMapper.selectByPrimaryKey(applyRefundDTO.getUserId());
        Optional.ofNullable(subscribers).orElseThrow(() -> new SelfRuntimeException(ResponseResultUtils.getResponseResultF("用户不存在")));
        final SelfOrder selfOrder = selfOrderMapper.selectOne(new LambdaQueryWrapper<SelfOrder>()
                .eq(SelfOrder::getId, applyRefundDTO.getOrderId())
                .eq(SelfOrder::getUserId, applyRefundDTO.getUserId())
        );
        Optional.ofNullable(selfOrder).orElseThrow(() -> new SelfRuntimeException(ResponseResultUtils.getResponseResultF("订单不存在")));

        if (selfOrder.getRefundStatus() == 1) {
            throw new SelfRuntimeException(ResponseResultUtils.getResponseResultF("订单已有一条退款记录，请勿重复申请。"));
        }
        if (selfOrder.getRefundStatus() == 2) {
            throw new SelfRuntimeException(ResponseResultUtils.getResponseResultF("该订单已同意退款，请勿重复申请。"));
        }
        if (selfOrder.getCarrierId() != 0 || (selfOrder.getStatus() != 2 && selfOrder.getStatus() != 3 && selfOrder.getStatus() != 4)) {
            throw new SelfRuntimeException(ResponseResultUtils.getResponseResultF("订单状态错误"));
        }

        selfOrder.setRefundStatus(1);
        selfOrder.setUpdateTime(DateUtil.date());
        selfOrderMapper.updateById(selfOrder);

        final SelfApplyRefund selfApplyRefund = SelfApplyRefund
                .builder()
                .applyMobile(subscribers.getMobile())
                .createTime(DateUtil.date())
                .reason(applyRefundDTO.getReason())
                .orderNo(selfOrder.getOrderNo())
                .build();
        selfApplyRefundMapper.insert(selfApplyRefund);
        return ResponseResultUtils.getResponseResultS("申请成功");
    }
}
